<template>
  <div class="system-log-container">
    <vab-query-form>
      <vab-query-form-top-panel>
        <el-form :inline="true" label-width="60px" :model="queryForm">
          <el-form-item label="周期">
            <el-date-picker
              v-model="queryForm.searchDate"
              end-placeholder="结束日期"
              start-placeholder="开始日期"
              type="datetimerange"
              value-format="yyyy-MM-dd HH:mm:ss"
              @change="changeDate"
            />
          </el-form-item>
          <el-form-item label="用户ID">
            <el-input
              v-model.trim="queryForm.user_id"
              clearable
              placeholder="请输入用户ID"
              @keyup.enter.native="queryData"
              @clear="queryData"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-top-panel>
    </vab-query-form>

    <el-table v-loading="listLoading" :data="list">
      <el-table-column align="center" label="日志ID" prop="id" width="100" />
      <el-table-column
        align="center"
        label="用户ID"
        prop="user_id"
        min-width="100"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="用户名"
        prop="user_name"
        min-width="180"
        show-overflow-tooltip
      />
      <!-- <el-table-column align="center" label="日志类型" prop="log_type" show-overflow-tooltip>
        <template #default="{ row }">
          <span v-if="row.log_type == 'login'">登陆日志</span>
          <span v-else-if="row.log_type == 'run'">运行日志</span>
          <span v-else>其它日志</span>
        </template>
      </el-table-column> -->
      <el-table-column
        align="center"
        label="登录IP"
        prop="ip"
        min-width="160"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="访问时间"
        prop="create_time_format"
        min-width="180"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="执行结果"
        prop="log_status"
        width="80"
      >
        <template #default="{ row }">
          <span v-if="row.log_status == 1">
            <span class="vab-dot vab-dot-success"><span></span></span>
            {{ row.executeResult }}
          </span>
          <span v-else>
            <span class="vab-dot vab-dot-error"><span></span></span>
            {{ row.executeResult }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="说明"
        prop="log_note"
        min-width="100"
        show-overflow-tooltip
      />
      <template #empty>
        <el-image
          class="vab-data-empty"
          :src="require('@/assets/empty_images/data_empty.png')"
        />
      </template>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.page"
      :layout="layout"
      :page-size="queryForm.perpage"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </div>
</template>

<script>
import { getList } from '@/api/systemLog'

export default {
  name: 'SystemLog',
  data() {
    return {
      list: [],
      listLoading: true,
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      queryForm: {
        account: '',
        searchDate: '',
        page: 1,
        perpage: 20,
      },
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    changeDate(value) {
      if (value) {
        this.queryForm.start_time = value[0]
        this.queryForm.end_time = value[1]
      } else {
        this.queryForm.start_time = null
        this.queryForm.end_time = null
      }
      this.queryData()
    },
    handleSizeChange(val) {
      this.queryForm.perpage = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.queryForm.page = val
      this.fetchData()
    },
    queryData() {
      this.queryForm.page = 1
      this.fetchData()
    },
    async fetchData() {
      this.listLoading = true
      const {
        data: { items, total },
      } = await getList(this.queryForm)
      this.list = items
      this.total = total
      this.listLoading = false
    },
  },
}
</script>
